package com.free.semantic.web.controller;

import com.free.semantic.web.configure.ControllerConfig;
import org.aspectj.util.FileUtil;
import org.springframework.web.bind.annotation.*;

import javax.annotation.PostConstruct;
import java.io.File;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

@CrossOrigin(methods = {RequestMethod.GET, RequestMethod.POST, RequestMethod.OPTIONS})
@RestController
@ControllerConfig
@RequestMapping("/file")
public class FileController {

    private Map<String, String> fileInfo = new HashMap<>();

    @PostConstruct
    void init() {
        fileInfo.put("businessPlan", "../semantic-net/businessPlan.md");
        fileInfo.put("introduce", "../semantic-net/readme.md");
        fileInfo.put("targetProduct", "../semantic-net/targetProduct.md");
        fileInfo.put("todo", "../semantic-net/todo.md");
    }

    @GetMapping("/{fileName}")
    public String business(@PathVariable("fileName") String fileName) throws IOException {
        if (!fileInfo.containsKey(fileName)) {
            return "";
        }
        File file = new File(fileInfo.get(fileName));
        String read = FileUtil.readAsString(file);
        return read;
    }

}
